package miniBanco.Gui;

import java.util.ArrayList;

import miniBanco.Dto.ClienteDTO;
import miniBanco.Exceptions.HandledException;
import MySwing.JAbm;
import MySwing.JFormCheckBox;
import MySwing.JFormComponent;
import MySwing.JFormField;
import MySwing.JFormNumberField;
import basics.DBManager;



public class ClientesForm extends JAbm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String TAG = ClientesForm.class.getSimpleName();
	private JFormNumberField formFieldId;	
	private JFormNumberField formFieldNumero;
	private JFormField formFieldRazonSocial;
	private JFormNumberField formFieldDni;
	private JFormNumberField formFieldCuit;
	private JFormCheckBox formFieldActivo;
	
	
	public ClientesForm() 
	{
		init();
	}

	
	public ClienteDTO getFormValues() {
		ClienteDTO cliente = new ClienteDTO();
		DBManager.Log(TAG, String.format("Cuit es: %s", formFieldNumero.getSpinner().getValue().toString()));
		cliente.setNroCliente(Integer.parseInt(formFieldNumero.getSpinner().getValue().toString()));
		cliente.setRazonSocial(formFieldRazonSocial.getTextField().getText());
		cliente.setCuit(Integer.parseInt(formFieldCuit.getSpinner().getValue().toString()));
		cliente.setDni(Integer.parseInt(formFieldDni.getSpinner().getValue().toString()));
		cliente.setActivo(formFieldActivo.getCheckBox().isSelected());
		cliente.setId(Integer.parseInt(formFieldId.getSpinner().getValue().toString()));
		return cliente;
	}

	public void setFormValues(ClienteDTO cliente) {
		formFieldId.getSpinner().setValue(cliente.getId());
		formFieldNumero.getSpinner().setValue(cliente.getNroCliente());
		formFieldRazonSocial.getTextField().setText(cliente.getRazonSocial());
		formFieldDni.getSpinner().setValue(cliente.getDni());
		formFieldCuit.getSpinner().setValue(cliente.getCuit());
		formFieldActivo.getCheckBox().setSelected(cliente.isActivo());
	}
	
	private void init() {
		ArrayList<JFormComponent> elements = new ArrayList<JFormComponent>();

		formFieldId = new JFormNumberField("Id", 0, 0, 999999999, 1);
		formFieldId.getSpinner().setEnabled(false);
		formFieldNumero = new JFormNumberField("NroCliente", 0, 0, 999999999, 1);
		formFieldDni = new JFormNumberField("Dni", 0, 0, 999999999, 1);
		formFieldCuit = new JFormNumberField("Cuit", 0, 0, 999999999, 1);
		formFieldRazonSocial = new JFormField("Razon Social",20,"Ingrese Razon social");
		formFieldActivo = new JFormCheckBox("Activo", true);
		elements.add(formFieldId);		
		elements.add(formFieldNumero);
		elements.add(formFieldRazonSocial);
		elements.add(formFieldDni);
		elements.add(formFieldCuit);
		elements.add(formFieldActivo);
		
		try {
			this.agregoComponentesAlPanel(elements);
		} catch (Exception ex) {
			DBManager.Error(ex);
			throw new HandledException("Error inesperado al agregar elementos al form. Ver Log");
		}
	}

}
